<template>
	<!--
	    author: 刘子恒
	    conAppDetailsCarouselMap: 轮播图
		conAppDetailsDetails:详细信息，会议室名称，会议室信息，预约/展示状态
		conAppDetailsPrice:价钱、可容纳人数、预约日期、时段，预约/展示状态
		conAppDetailsCheck:选择套餐
		conAppDetaisInteriorImg:内景图
		该页面根据状态分为 展示页面和预约页面，需要给子组件传递
		1、状态值：展示/预约     （从上层页面获取）
		2、会议室内容对象：
		  id
		  name
		  carouselImg（会议室展示轮播图3张）
		  title
		  details
		  price
		  peopleNumber
		  data（预约日期，如果是展示则传入已预约的日期，如果是预约，则传入当前日期到一周内截止）
		  time（预约时段，如果是展示则传入已预约的时段，如果是预约，则传入查询到的可预约时段）
		  serviceItems（服务套餐，如果是展示则传入已预约的套餐，如果是预约，则传入查询到的套餐）
		  checkArg (服务套餐id，如果是展示则传入已预约的套餐id，如果是预约，则传入-1)
	-->
	<view class="Details-container">
		<con-app-details-carousel-map :swiperArray="givenArray.interiorImg"></con-app-details-carousel-map>
		<con-app-details-details v-on:confirm="confirm" :title="givenArray.name" :details="givenArray.details" :freeTime="freeTime" :state="state">
		</con-app-details-details>
		<con-app-details-price ref="dateDetails" :price="givenArray.price" :peopleNumber="givenArray.peopleNumber" :roomDetails="givenArray" :state="state" :roomId="roomId">
		</con-app-details-price>
		
		<con-app-details-check ref="serviceDetails" @checkArgs="checkArgs" checkArg="" :serviceItems="givenArray.serviceItems" :state="state"></con-app-details-check>
		<!-- <con-app-detais-interior-img :interiorImage="givenArray.interiorImg"></con-app-detais-interior-img> -->
	</view>
</template>

<script>
	import {
		ONLINE_SERVER_ADDRESS_BASE,
	} from "../../../util/network.js"
	/**
	 * author: 李文昊
	 * description: 传数组时检验时不要忘记【0】
	 */
	export default {
		data() {
			return {
				token:'',
				state:"预约",
				roomArg: {
					'id': 0,
					'title': "",
					'date': "",
					'time': "",
					'serviceId': -1,
					'serviceName': "未选择套餐",
					'price': 0,
				},
				servicePrice: 0,
				givenArray: {
						id: 0,
						name: "101",
						title: "101豪华会议室",
						details: "会场环境优雅，欧式座椅，设施齐全，内设六重隔音，层高7米，装有室内无影灯，投影仪、调音台、6台大型音响、中央空调等。可完成对各种图文信息（包括各种软体的使用、DVD/CD碟片、录像带、各种实物、声音）的播放功能；实现多功能厅的现场扩音、播音，配合大屏幕投影系统，提供优良的视听效果。",
						price: 325,
						peopleNumber: "5-10",
						availableDays:0,
						serviceItems: [{
								id:0,
								value: "A",
								name: "茶水服务+投影+空调+两位服务员",
								price: 100
							},
							{
								id:1,
								value: "B",
								name: "选项服务二",
								price: 120
							},
							{
								id:2,
								value: "C",
								name: "选项服务三",
								price: 130
							},
							{	
								id:3,
								value: "D",
								name: "选项服务四",
								price: 140
							},
							{
								id:4,
								value: "E",
								name: "选项服务五",
								price: 100
							},
							{
								id:5,
								value: "F",
								name: "选项服务六",
								price: 100
							},
						],
						interiorImg: [{
								name: "内景图一",
								imgUrl: require('@/static/interior1.png')
							},
							{
								name: "内景图二",
								imgUrl: require('@/static/interior2.png')
							},
							{
								name: "内景图三",
								imgUrl: require('@/static/interior3.png')
							},
						]
					},
				availableDurationList:[],
				roomId:"",
				freeTime:0,
			}
		},
		onLoad(option) {
			//此处根据会议室ID查询详细信息
			this.roomId = option.eventId
			// this.roomId = "5"
			this.token = uni.getStorageSync("token")
			this.getFreeTime()
			this.getMeetingRoomInfo(this.roomId)
			this.getRoomCombo()
		},
		methods: {
			getFreeTime(){
				let _this = this
				let userInfo = uni.getStorageSync('userinfo')
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/user/show/` + userInfo.id,
					header: {
						'Authorization': this.token,
					},
					method:'GET',
					success: function (res) {
						console.log("查询个人信息",res);
						if(res.data.code == 200){
							_this.freeTime = res.data.data.user.meetingRoomFeeResidueQuota
						}
					},
					fail: (res) => {
						console.log("查询个人信息")
					}
				})
			},
			async loadImg(chargeFileId){
				// let _this = this
				const res = await uni.downloadFile({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/file/download/` + chargeFileId,
					header: {
						'Authorization': this.token,
					},
				})
				return new Promise((resolve, reject) => {
				        resolve(res[1].tempFilePath)
				})
			},
			getRoomCombo(){
				let _this = this
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/meeting-room-combo/list`,
					header: {
						'Authorization': this.token,
					},
					data:{
						pageNo:1,
						pageSize:10,
					},
					method:'GET',
					success: function (res) {
						console.log("查询会议室套餐",res);
						if(res.data.code == 200){
							_this.givenArray.serviceItems = res.data.data.meetingRoomComboList
						}
					},
					fail: (res) => {
						console.log("查询会议室失败")
					}
				})
			},
			getMeetingRoomInfo(roomId){
				let _this = this
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/meeting-room/show/` + roomId,
					header: {
						'Authorization': this.token,
					},
					method:'GET',
					success: function (res) {
						console.log("查询单个会议室",res);
						if(res.data.code == 200){
							_this.givenArray.id = res.data.data.id
							_this.givenArray.name = res.data.data.houseNo
							_this.givenArray.title = res.data.data.name
							_this.givenArray.details = res.data.data.intro
							_this.givenArray.price = res.data.data.price
							_this.givenArray.peopleNumber = res.data.data.capacity.toString()
							_this.givenArray.interiorImg = []
							_this.givenArray.availableDays = res.data.data.maxReserveDay
							_this.$refs.dateDetails.initDate(res.data.data.maxReserveDay - 1)
							let pictureList = res.data.data.picture.split(',')
							for (var i = 0; i < pictureList.length; i++) {
								let temp = {
									name: "内景图",
									imgUrl: '',
								}
								temp.imgUrl = ONLINE_SERVER_ADDRESS_BASE + '/file/preview/' + pictureList[i]
								_this.givenArray.interiorImg.push(temp)
							}	
						}
					},
					fail: (res) => {
						console.log("查询会议室失败")
					}
				})
			},
			checkArgs(serviceId, serviceName, price) {
				console.log(serviceId)
				console.log(serviceName)
				this.roomArg.serviceId = serviceId
				this.roomArg.serviceName = serviceName
				this.servicePrice = price
			},
			confirm() {
				//确认预约
				let dateInfo = this.$refs.dateDetails.getCurrentInfo()
				if(dateInfo == -1){
					uni.showModal({
						content:'请选择预约日期及时间',
						showCancel:false
					})
					return -1
				}
				if(dateInfo == -2){
					uni.showModal({
						content:'请重新选择预约日期及时间',
						showCancel:false
					})
					return -1
				}
				// console.log("title",this.givenArray.id)               //会议室ID
				// console.log("title",this.givenArray.title)            //会议室title
				// console.log("title",this.$refs.dateDetails.getCurrentInfo())               //预约日期及时段（时段为查询到的可预约时段）
				this.roomArg.id = this.givenArray.id                     //会议室ID
				this.roomArg.title = this.givenArray.name              //会议室title
				this.roomArg.date = dateInfo.currentDate                //预约日期
				this.roomArg.time = dateInfo.time                       //预约时段
				this.roomArg.hours = dateInfo.hours
				let useFreeTime = 0
				if(this.isWeekend(this.roomArg.date) || this.isNoWorktime(this.roomArg.time)){ //应甲方要求，非工作日无法使用免费时间
					useFreeTime = 0
					console.log("是周末")
				}else{
					if(this.roomArg.hours >= this.freeTime){
						useFreeTime = this.freeTime
					}else{
						useFreeTime = this.roomArg.hours
					}
					console.log("非周末")
				}
				this.roomArg.useFreeTime = useFreeTime
				this.roomArg.price = dateInfo.allPrice + this.servicePrice - dateInfo.unitPrice * useFreeTime                //总费用
				this.roomArg.price = this.roomArg.price.toFixed(2) 
				if(this.roomArg.price < 0.001){
					this.roomArg.price = 0
				}
				uni.redirectTo({
					url:'../details/details?id=待分配' + '&state=' + '待申请' + '&roomArg=' + encodeURIComponent(JSON.stringify(this.roomArg))
				})
				
			},
			isWeekend(date){
				var _date = new Date(date);
				var num = _date.getDay(_date)
				if(num == 0 || num == 6){
					return true
				}else{
					return false
				}
			},
			isNoWorktime(time){
				console.log('qzw-building',time)
				var startTime = parseInt(time.split('~')[0].split(':')[0])
				var endTime = parseInt(time.split('~')[1].split(':')[0])
				if(startTime < 9 || endTime > 18){
					return true
				}else{
					return false
				}
			}
		}
	}
</script>

<style>
	.Details-container {
		padding-bottom: 120rpx;
		background-color: #FFFFFF;
	}
</style>
